package com.mumulx.check.service.service.impl;

import com.mumulx.check.mapper.SubmitRecordMapper;
import com.mumulx.check.model.PageHelp;
import com.mumulx.check.model.SubmitFileRecord;
import com.mumulx.check.model.SubmitRecord;
import com.mumulx.check.redis.RedisUtil;
import com.mumulx.check.redis.util.FastDfsClientUtil;
import com.mumulx.check.service.service.SubmitRecordService;
import com.mumulx.check.service.util.HelpDev;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.json.XML;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/*
 *  Created by IntelliJ IDEA.
 *  User: 木木
 *  Date: 2020/4/28
 *  Time: 22:51
 */
@Service("submitRecordService")
@Slf4j
public class SubmitRecordServiceImpl implements SubmitRecordService {

    //private static Logger logger = LoggerFactory.getLogger(SubmitRecordServiceImpl.class);

    @Resource
    SubmitRecordMapper submitRecordMapper;

    @Resource
    HelpDev helpDev;
    @Resource
    RedisUtil redisUtil;

    @Resource
    FastDfsClientUtil fastDfsClientUtil;
    @Override
    public int addSR(SubmitRecord submitRecord) {
        /*获取当前时间*/
        LocalDateTime localDateTime = LocalDateTime.now();
        /*时间格式化*/
        String dateTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        submitRecord.setSrDate(dateTime);
        submitRecord.setSrEnable(true);

        submitRecordMapper.addSR(submitRecord);
        return submitRecord.getSrId();
    }

    @Override
    public List<SubmitRecord> queryRecordListByUserId(int userId, int currentPage,boolean isEnable) {
        int pageSize = PageHelp.pageSize;
        int startIndex = (currentPage - 1) * pageSize;
        int a=0;
        if (isEnable)a=1;
        return submitRecordMapper.queryRecordListByUserId(userId, startIndex, pageSize,a);

    }

    /*获取用户测试记录总数*/
    @Override
    public int queryRecordCountByUserId(int userId,boolean isEnable) {
        int a=0;
        if (isEnable){
            a = 1;
        }
        return submitRecordMapper.queryRecordCountByUserId(userId,a);
    }

    /*删除本次测试记录，根据测试记录id*/
    @Override
    public void deleteRecordBySrId(int srId) {
        log.info("开始删除测试记录");
        //删除记录
        submitRecordMapper.deleteRecordBySrId(srId);
        log.info("测试记录删除成功");

    }

    /*根据测试记录id获取测试记录所有信息*/
    @Override
    public SubmitRecord queryRecordBySrId(int srId) {
        return submitRecordMapper.queryRecordBySrId(srId);
    }





    /*
     * 获取测试结果
     * @author mumulx
     * @creed: mumulx编写
     * @email: 2606964863@qq.com
     * @date 2020/11/5 9:35
     * @param userId
     * @param submitRecord
     * @param submitFileRecord
     * @return java.util.Map<java.lang.String,java.lang.String>
     *
     */
    @Override
    public Map<String, String> getThePrtTestResult(int userId, SubmitRecord submitRecord, SubmitFileRecord submitFileRecord) throws IOException {


        byte[] bytes = fastDfsClientUtil.download_bytes(submitFileRecord.getSfrXmlFile());
        JSONObject jsonObject = XML.toJSONObject(new String(bytes));

        /*返回结果*/
        Map<String, String> maps = new HashMap<>();

        maps.put("jpgFilePath", submitFileRecord.getSfrPngFile());

        maps.put("xmlFile", jsonObject.toString().replaceAll("chk:", "chk_"));

        return maps;
    }

    @Override
    public void disableRecordBySrId(int recordId) {
        submitRecordMapper.disableRecordBySrId(recordId);
    }

    @Override
    public List<SubmitRecord> queryExpiredTimeList() {
        return submitRecordMapper.queryExpiredTimeList();
    }

}
